package com.cc.moxingvoice.lock;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;

import com.cc.moxingvoice.R;
import com.cc.moxingvoice.base.BaseActivity;
import com.cc.moxingvoice.base.BaseLock;
import com.cc.moxingvoice.constant.LocalPath;
import com.cc.moxingvoice.databinding.ActivityAddVoicePackageBinding;
import com.cc.moxingvoice.ui.voice.VoiceActivity;
import com.cc.moxingvoice.utils.FilesUtil;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * 创建日期：2018/3/4 on 10:31
 * 描述：添加语音包
 * 作者：郭士超
 * QQ：1169380200
 */

public class AddVoicePackageLock extends BaseLock<ActivityAddVoicePackageBinding> {

    private static final int PHOTO_PICK = 1;
    private static final int PHOTO_CUT = 2;

    private String title;
    private String oldTitle;
    private Uri uri;
    private Bitmap mBitmap;

    private boolean edit;

    public AddVoicePackageLock(Context context, ActivityAddVoicePackageBinding binding) {
        super(context, binding);
    }

    public AddVoicePackageLock(Context context, ActivityAddVoicePackageBinding binding, Bundle bundle) {
        super(context, binding, bundle);
    }

    @Override
    protected void init() {
        if (mBundle != null && mBundle.getBoolean("edit")) {
            edit = true;
            oldTitle = mBundle.getString("title");
            title = oldTitle;
            try {
                String picPath = LocalPath.VOICE_PATH + title + "/" + LocalPath.VOICE_ICON;
                if (FilesUtil.isFileExists(picPath)) {
                    FileInputStream fis = new FileInputStream(picPath);
                    mBitmap = BitmapFactory.decodeStream(fis);
                    mBinding.icon.setImageBitmap(mBitmap);
                } else {
                    mBinding.icon.setImageResource(R.drawable.icon);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    public void appBarRightDone() {
        if (title.isEmpty()) {
            showToast(getContext().getString(R.string.no_voice_package));
            return;
        }
        if (edit) {
            String oldPath = LocalPath.VOICE_PATH + oldTitle + "/";
            String newPath = LocalPath.VOICE_PATH + title + "/";
            FilesUtil.filesRename(oldPath, newPath);
            if (mBitmap != null) {
                FilesUtil.savePic(newPath, LocalPath.VOICE_ICON, mBitmap);
            }
        } else {
            String voicePackagePath = LocalPath.VOICE_PATH + title + "/";
            FilesUtil.mkdirPath(voicePackagePath);
            if (mBitmap != null) {
                FilesUtil.savePic(voicePackagePath, LocalPath.VOICE_ICON, mBitmap);
            }
            Bundle bundle = new Bundle();
            bundle.putString("title", title);
            bundle.putBoolean("manage", true);
            startActivity(VoiceActivity.class, bundle);
        }
        finish();
    }

    public void selectIcon(View view) {
        // "从相册选择"按钮被点击了
        Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
        // 如果限制上传到服务器的图片类型时可以直接写如："image/jpeg 、 image/png等的类型"
        pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        ((BaseActivity) getContext()).startActivityForResult(pickIntent, PHOTO_PICK);
    }

    public void deleteIcon(View view) {
        mBitmap = null;
        mBinding.icon.setImageResource(R.drawable.icon);
    }

    // 裁剪图片方法实现
    public void photoPick(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 512);
        intent.putExtra("outputY", 512);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
        ((BaseActivity) getContext()).startActivityForResult(intent, PHOTO_CUT);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case PHOTO_PICK:
                    // 调用相册选择返回的图片
                    uri = data.getData();
                    photoPick(uri);
                    break;
                case PHOTO_CUT:
                    try {
                        mBitmap = BitmapFactory.decodeStream(getContext().getContentResolver().openInputStream(uri));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    mBinding.icon.setImageBitmap(mBitmap);
                    break;
            }
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}
